//
//  KZPronounceImproveVC.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/8/22.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZPronounceImproveVC.h"

// net
#import "KZPronounceNetApi.h"

// view
#import "KZPronounceReviewListScroller.h"       // 需复习音标
#import "KZShadowButton.h"                      // 按钮
#import "KZAlertView.h"                         // 弹窗
// vc
#import "KZPronounceMakeTestVC.h"               // 开始测试
#import "KZUserCenterVC.h"
#import "KZPronounceMakeTestVC_version2.h"

@interface KZPronounceImproveVC ()

@property (nonatomic, strong) KZPronounceReviewListScroller *reviewListScroller;

@end

@implementation KZPronounceImproveVC

- (void)dealloc {
    
    NSLog(@"vc销毁了");
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.layer.contentsGravity = kCAGravityResizeAspectFill;
    self.view.layer.contents = (__bridge id)[UIImage imageNamed:@"kz-全图大背景"].CGImage;
    
    [self setupNaviUI];
    [self setupUI];
    
    [self.navigationController zk_registerBackInteractive:nil gestureConfig:nil];
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self setNaviAttributeWithTranslucent:true stateBarLightContent:true];
    // 获取待复习音标信息
    [self getStandarPronouncationInfo];
}
- (void)setupNaviUI {
    
//    self.title = @"标准发音强化训练";
//    [self addLeftBackBarButtonItem];
    
    NSString *text = @"标准发音强化训练";
    
    UIControl *backView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    [backView addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
    
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//    backImageView.image = [UIImage imageNamed:@"kz-关闭"];
    backImageView.image = [UIImage imageNamed:@"LS-nav_back"];
    [backView addSubview:backImageView];
    [backImageView sizeToFit];
    backImageView.centerY = backView.height / 2;
    
//    UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(backImageView.right+5, 10, 20, 20)];
//    iconView.image = [UIImage imageNamed:@"kz-左上角标题图标"];
//    [backView addSubview:iconView];
//    [iconView sizeToFit];
//    iconView.centerY = backView.height / 2;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(backImageView.right+5, 0, [text getSizeOfBlodString:20].width, 40)];
    label.adjustsFontSizeToFitWidth = true;
    label.numberOfLines = 1;
    label.text = text;
    label.font = B_FONT(20);
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [backView addSubview:label];
    
    backView.width = label.right;
    
    UIButton *noteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    noteButton.frame = CGRectMake(0, 0, 17, 17);
    [noteButton setImage:[UIImage imageNamed:@"kz-问号"] forState:UIControlStateNormal];
    [noteButton addTarget:self action:@selector(note) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:noteButton];
}
- (void)setupUI {
    
    // 需复习
    _reviewListScroller = [KZPronounceReviewListScroller new];
    [self.view addSubview:_reviewListScroller];
    [_reviewListScroller mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(H(40));
        } else {
            make.top.mas_equalTo(H(100));
        }
        
        make.height.equalTo(_reviewListScroller.mas_width).multipliedBy(0.8);
    }];
    __weak typeof(self) wself = self;
    _reviewListScroller.clickBlock = ^(NSInteger index) {
        [wself pronounceClick:index];
    };

    // 按钮
    KZShadowButton *beginButton = [[KZShadowButton alloc] init:^(UIButton *button) {
        button.backgroundColor = KZThemeColor;
        [button setTitle:@"开始训练" forState:UIControlStateNormal];
    }];
    [beginButton addTarget:self action:@selector(begin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:beginButton];
    [beginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view).multipliedBy(0.5);
        make.height.mas_equalTo(W(40));
        if (@available(ios 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-W(30));
        } else {
            make.bottom.mas_equalTo(-W(30));
        }
    }];
    
//    // 标注
//    UILabel *noteLabel = [[UILabel alloc] init];
//    noteLabel.numberOfLines = 0;
//    noteLabel.text = @"系统智能诊断出较弱的音标，通过发音训练，快速提升发音水平";
//    noteLabel.font = FONT(12);
//    noteLabel.textAlignment = NSTextAlignmentCenter;
//    noteLabel.textColor = [UIColor lightGrayColor];
//    [self.view addSubview:noteLabel];
//    [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@10);
//        make.right.equalTo(@-10);
//        make.bottom.equalTo(beginButton.mas_top).offset(-20);
//    }];
//    // 标注
//    UILabel *note2Label = [[UILabel alloc] init];
//    note2Label.text = @"当前掌握较弱的音标";
//    note2Label.font = FONT(16);
//    note2Label.textAlignment = NSTextAlignmentCenter;
//    note2Label.textColor = [UIColor blackColor];
//    [self.view addSubview:note2Label];
//    [note2Label mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(noteLabel);
//        make.bottom.equalTo(noteLabel.mas_top).offset(-5);
//    }];
}

#pragma mark - Net
- (void)getStandarPronouncationInfo {
    
    [MBProgressHUD showLoadind:@"正在查询待复习音标，请稍候..."];
    
    KZPronounceNetApi *api = [KZPronounceNetApi new];
    api.requestParam = @{@"stageNo":KGetUserDefaults(@"TestLayer"),
                         @"schoolId":DR_UserInfo.SchoolID,
                         @"stuId":DR_UserInfo.UserID,};
    [api getStandarPronouncationInfo:^ (NSArray *list) {
        
        [MBProgressHUD hideHud];
        
        _reviewListScroller.soundmarkList = list;
        [_reviewListScroller setNeedsLayout];
        [_reviewListScroller layoutIfNeeded];
    } failuer:^(NSString *error) {
        
        [MBProgressHUD showFailureWithText:error];
    }];
}

#pragma mark - 事件
- (void)back {
    [self dismissViewControllerAnimated:true completion:nil];
}
// 问号备注
- (void)note {
    
    // 弹框提示
    NSMutableAttributedString *subtitle = [[NSMutableAttributedString alloc] initWithString:@"系统智能诊断较弱音标，通过发音训练，快速提升发音水平"];
    
    KZAlertView *alert = [KZAlertView new];
    [alert showWithTitle:@"温馨提示" subtitle:subtitle submitTypes:@[@1] submitButtonsConfig:^(NSArray<UIButton *> *buttons) {
        
        [buttons.firstObject setTitle:@"好的" forState:UIControlStateNormal];
    } click:^(NSInteger index) {
    
    } destroyAtClickIndex:@[@0]];
}

// 点击了某个音标
- (void)pronounceClick:(NSInteger)index {

    KZPronounceMakeTestVC_version2 *vc = [KZPronounceMakeTestVC_version2 new];
    vc.soundmarkList = _reviewListScroller.soundmarkList;
    vc.index = index;
    [self push:vc];
}
// 开始训练，从第0个开始训练
- (void)begin {
    
    if (_reviewListScroller.soundmarkList.count == 0) {
        [MBProgressHUD showOnlyText:@"无待复习音标!"];
        return;
    }
    
    KZPronounceMakeTestVC_version2 *vc = [KZPronounceMakeTestVC_version2 new];
    vc.soundmarkList = _reviewListScroller.soundmarkList;
    vc.index = 0;
    [self push:vc];

}

@end
